<?php

namespace App\Http\Model;

/**
 * @desc 校验数据类型
 * Class FormatCheck
 * @package app\http\model
 */
class FormatCheck
{
    //手机号
    static function checkPhone($needle)
    {
        if(empty($needle))
            return false;
        $search = '/^0?1[3|4|5|6|7|8|9][0-9]\d{8}$/';
        if (preg_match($search, $needle)) {
            return true;
        } else {
            return false;
        }
        
    }
    //验证码
    static function checkSmsCode($needle = '',$fixLen = 6){
        if (!$needle)
            return false;
        if (!is_int($needle))
            return false;
        if (strlen($needle) != $fixLen)
            return false;
        return true;
    }
    //时间戳
    static function checkTimeStamp($needle = ''){
        if(!$needle)
            return false;
        if(strtotime(date('Y-m-d H:i:s', $needle)) == $needle) {
            return true;
        } else {
            return false;
        }
    }
    //int
    static function checkInt($needle = '', $min = 0, $max = 1000000 )
    {
        if (!$needle)
            return false;
        if (!is_int($needle))
            return false;
        if ((strlen($needle) < $min) || (strlen($needle) > $max))
            return false;
        return true;
    }
    
    //string
    static function checkString($needle = '', $min = 0, $max = 1000000)
    {
        if (!$needle)
            return false;
        if (!is_string($needle))
            return false;
        if ((strlen($needle) < $min) || (strlen($needle) > $max))
            return false;
        return true;
    }
    
    //密码
    static function checkPassword($needle = '',$min = 6, $max = 16){
        if(!strlen($needle)){
            return false;
        }
        if(strlen($needle) < $min || (strlen($needle) > $max)){
            return false;
        }
        if(!preg_match("/^[a-z\d]*$/i",$needle))
        {
            return false;//有数字有字母 ";
        }
        return true;
    }
    
    //判断网址
    static function checkUrl($needle = '', $min = 1){
        if(!strlen($needle)){
            return false;
        }
        if(strlen($needle) < $min){
            return false;
        }
        if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%
=~_|]/i",$needle)) {
            return false;
        }
        return true;
        
    }
    
}
